package com.rerun.common.rabbitmq;

import cn.jiguang.common.utils.StringUtils;
import org.springframework.amqp.core.MessageListener;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * The type Rabbit mq consumer.
 *
 * @author dax.
 * @version v1.0
 * @since 2017 /11/17 13:10
 */
@Component
public class RabbitMqConsumer {

    private String queueName;
    private String routingKey;
    private int onOfConsumer;

    private MessageListener messageConsumer;
    @Resource
    private SimpleMessageListenerContainer simpleMessageListenerContainer;


    /**
     * Instantiates a new Rabbit mq consumer.
     */
    public RabbitMqConsumer() {
    }


    /**
     * Instantiates a new Rabbit mq consumer.
     *
     * @param queueName    the queue name
     * @param routingKey   the routing key
     * @param onOfConsumer the on of consumer
     * @throws Exception the exception
     */
    public RabbitMqConsumer(String queueName, String routingKey, int onOfConsumer) throws Exception {
        this.queueName = queueName;
        this.routingKey = routingKey;
        this.onOfConsumer = onOfConsumer;
        initialize();
    }

    /**
     * Initialize.
     *
     * @throws Exception the exception
     */
    public void initialize() throws Exception {
        if (StringUtils.isNotEmpty(queueName) && this.onOfConsumer > 0 && Objects.nonNull(messageConsumer)) {
            simpleMessageListenerContainer.setQueueNames(this.queueName);
            simpleMessageListenerContainer.setConcurrentConsumers(this.onOfConsumer);
            simpleMessageListenerContainer.setReceiveTimeout(100000);
            simpleMessageListenerContainer.setMessageListener(messageConsumer);
            simpleMessageListenerContainer.start();
        }else {

            throw new Exception("check consumer config");
        }
    }

    /**
     * Gets on of consumer.
     *
     * @return the on of consumer
     */
    public int getOnOfConsumer() {
        return onOfConsumer;
    }

    /**
     * Sets on of consumer.
     *
     * @param onOfConsumer the on of consumer
     */
    public void setOnOfConsumer(int onOfConsumer) {
        this.onOfConsumer = onOfConsumer;
    }

    /**
     * Gets queue name.
     *
     * @return the queue name
     */
    public String getQueueName() {
        return queueName;
    }

    /**
     * Sets queue name.
     *
     * @param queueName the queue name
     */
    public void setQueueName(String queueName) {
        this.queueName = queueName;
    }

    /**
     * Gets routing key.
     *
     * @return the routing key
     */
    public String getRoutingKey() {
        return routingKey;
    }

    /**
     * Sets routing key.
     *
     * @param routingKey the routing key
     */
    public void setRoutingKey(String routingKey) {
        this.routingKey = routingKey;
    }

    public MessageListener getMessageConsumer() {
        return messageConsumer;
    }

    public void setMessageConsumer(MessageListener messageConsumer) {
        this.messageConsumer = messageConsumer;
    }
}
